// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   designProcedure_draft_ok.java

package draft.manufacture;

import include.nseer_cookie.exchange;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import validata.ValidataNumber;

public class designProcedure_draft_ok extends HttpServlet
{

	public designProcedure_draft_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		nseer_db_backup1 manufacture_db = new nseer_db_backup1(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		try
		{
			if (manufacture_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String design_ID = request.getParameter("design_ID");
				String procedure_namen[] = request.getParameterValues("procedure_name");
				String procedure_amount = request.getParameter("procedure_amount");
				int num = Integer.parseInt(procedure_amount);
				int m = 0;
				int n = 0;
				for (int j = 1; j < procedure_namen.length; j++)
				{
					String sql3 = (new StringBuilder("select * from manufacture_design_procedure_details where design_ID='")).append(design_ID).append("' and procedure_name='").append(procedure_namen[j]).append("'").toString();
					ResultSet rs3 = manufacture_db.executeQuery(sql3);
					if (rs3.next())
						m++;
				}

				if (num == 0 && procedure_namen.length == 1)
					response.sendRedirect((new StringBuilder("draft/manufacture/designProcedure_ok_a.jsp?design_ID=")).append(design_ID).toString());
				else
				if (m != 0)
				{
					response.sendRedirect((new StringBuilder("draft/manufacture/designProcedure_ok_b.jsp?design_ID=")).append(design_ID).toString());
				} else
				{
					for (int i = 1; i <= num; i++)
					{
						String tem_labour_hour_amount = (new StringBuilder("labour_hour_amount")).append(i).toString();
						String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
						String tem_amount_unit = (new StringBuilder("amount_unit")).append(i).toString();
						String labour_hour_amount = request.getParameter(tem_labour_hour_amount);
						if (labour_hour_amount.equals(""))
							labour_hour_amount = "0";
						String amount_unit = request.getParameter(tem_amount_unit);
						String cost_price2 = request.getParameter(tem_cost_price);
						if (cost_price2.equals(""))
							cost_price2 = "0";
						StringTokenizer tokenTO3 = new StringTokenizer(cost_price2, ",");
						String cost_price;
						String cost_price1;
						for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
							cost_price1 = tokenTO3.nextToken();

						if (!validata.validata(labour_hour_amount) || !validata.validata(cost_price))
							n++;
						if (amount_unit.indexOf("'") != -1 || amount_unit.indexOf("\"") != -1 || amount_unit.indexOf(",") != -1)
							n++;
					}

					String labour_hour_amountn[] = request.getParameterValues("labour_hour_amount");
					String cost_pricen[] = request.getParameterValues("cost_price");
					for (int j = 1; j < procedure_namen.length; j++)
						if (!validata.validata(labour_hour_amountn[j]) || !validata.validata(cost_pricen[j]))
							n++;

					if (n == 0)
					{
						String product_ID = request.getParameter("product_ID");
						String product_name = request.getParameter("product_name");
						String designer = request.getParameter("designer");
						String bodyc = new String(request.getParameter("procedure_describea").getBytes("UTF-8"), "UTF-8");
						String procedure_describea = exchange.toHtml(bodyc);
						String register_time = request.getParameter("register_time");
						String register = request.getParameter("register");
						try
						{
							String sql = (new StringBuilder("update manufacture_design_procedure set design_ID='")).append(design_ID).append("',product_ID='").append(product_ID).append("',product_name='").append(product_name).append("',register_time='").append(register_time).append("',register='").append(register).append("',designer='").append(designer).append("',procedure_describe='").append(procedure_describea).append("' where design_ID='").append(design_ID).append("'").toString();
							manufacture_db.executeUpdate(sql);
							double cost_price_sum = 0.0D;
							for (int i = 1; i <= num; i++)
							{
								String tem_procedure_name = (new StringBuilder("procedure_name")).append(i).toString();
								String tem_procedure_ID = (new StringBuilder("procedure_ID")).append(i).toString();
								String tem_procedure_describe = (new StringBuilder("procedure_describe")).append(i).toString();
								String tem_labour_hour_amount = (new StringBuilder("labour_hour_amount")).append(i).toString();
								String tem_amount_unit = (new StringBuilder("amount_unit")).append(i).toString();
								String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
								String procedure_name = request.getParameter(tem_procedure_name);
								String procedure_ID = request.getParameter(tem_procedure_ID);
								String procedure_describe = request.getParameter(tem_procedure_describe);
								String labour_hour_amount = request.getParameter(tem_labour_hour_amount);
								String amount_unit = request.getParameter(tem_amount_unit);
								String cost_price2 = request.getParameter(tem_cost_price);
								if (labour_hour_amount.equals(""))
									labour_hour_amount = "0";
								if (cost_price2.equals(""))
									cost_price2 = "0";
								StringTokenizer tokenTO3 = new StringTokenizer(cost_price2, ",");
								String cost_price;
								String cost_price1;
								for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
									cost_price1 = tokenTO3.nextToken();

								double subtotal = Double.parseDouble(cost_price) * Double.parseDouble(labour_hour_amount);
								cost_price_sum += subtotal;
								String sql1 = (new StringBuilder("update manufacture_design_procedure_details set procedure_ID='")).append(procedure_ID).append("',details_number='").append(i).append("',procedure_name='").append(procedure_name).append("',procedure_describe='").append(procedure_describe).append("',labour_hour_amount='").append(labour_hour_amount).append("',cost_price='").append(cost_price).append("',amount_unit='").append(amount_unit).append("',subtotal='").append(subtotal).append("' where design_ID='").append(design_ID).append("' and procedure_name='").append(procedure_name).append("'").toString();
								manufacture_db.executeUpdate(sql1);
							}

							String procedure_IDn[] = request.getParameterValues("procedure_ID");
							String procedure_describen[] = request.getParameterValues("procedure_describe");
							String amount_unitn[] = request.getParameterValues("amount_unit");
							for (int j = 1; j < procedure_namen.length; j++)
							{
								StringTokenizer tokenTO2 = new StringTokenizer(cost_pricen[j], ",");
								String cost_price;
								String cost_price1;
								for (cost_price = ""; tokenTO2.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
									cost_price1 = tokenTO2.nextToken();

								double subtotal = Double.parseDouble(cost_price) * Double.parseDouble(labour_hour_amountn[j]);
								cost_price_sum += subtotal;
								int details_number = num + j;
								String sql4 = (new StringBuilder("insert into manufacture_design_procedure_details(design_ID,details_number,procedure_ID,procedure_name,procedure_describe,labour_hour_amount,cost_price,amount_unit,subtotal) values ('")).append(design_ID).append("','").append(details_number).append("','").append(procedure_IDn[j]).append("','").append(procedure_namen[j]).append("','").append(procedure_describen[j]).append("','").append(labour_hour_amountn[j]).append("','").append(cost_price).append("','").append(amount_unitn[j]).append("','").append(subtotal).append("')").toString();
								manufacture_db.executeUpdate(sql4);
							}

							String sql2 = (new StringBuilder("update manufacture_design_procedure set cost_price_sum='")).append(cost_price_sum).append("' where design_ID='").append(design_ID).append("'").toString();
							manufacture_db.executeUpdate(sql2);
						}
						catch (Exception ex)
						{
							ex.printStackTrace();
						}
						response.sendRedirect("draft/manufacture/designProcedure_ok.jsp?finished_tag=0");
					} else
					{
						response.sendRedirect((new StringBuilder("draft/manufacture/designProcedure_ok_c.jsp?design_ID=")).append(design_ID).toString());
					}
				}
				manufacture_db.commit();
				manufacture_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
